# 自定义共用的工具类
from flask import session, current_app, g
from functools import wraps


def do_index_class(index):
    """
    返回指定索引对应的类名
    :param index:
    :return:
    """
    if index == 1:
        return 'first'
    elif index == 2:
        return  'second'
    elif index == 3:
        return 'third'

    return ""


#登陆装饰器
def user_login_data(view_func):
    # 使用functools.wraps 去装饰内层函数, 可以保存当前装饰器去装饰的函数的__name__ 的值不变
    @wraps(view_func)
    def wrapper(*args,**kwargs):

        # 获取用户编号
        user_id = session.get('user_id', None)

        # 查询用户对象
        user = None
        if user_id:
            try:
                from info.models import User
                user = User.query.get(user_id)
            except Exception as e:
                current_app.logger.error(e)

        #保存用户对象到g
        g.user = user

        return view_func(*args,**kwargs)
    return wrapper